//
//  NSString+TYHelper.m
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import "NSString+TYHelper.h"
#import <arpa/inet.h>
#import <netinet/in.h>
@implementation NSString (TYHelper)

- (BOOL)ty_isEmpty {
    return [self ty_isEmptyShouldTrimBlankCharacter:YES];
}

- (BOOL)ty_isEmptyShouldTrimBlankCharacter:(BOOL)trimFlag {
    if ([self length] == 0) {
        return YES;
    }
    
    if (trimFlag) {
        return ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0);
    }
    
    return NO;
}

+ (BOOL)ty_stringIsEmpty:(NSString *)string {
    return [self ty_stringIsEmpty:string trimBlankCharacter:YES];
}

+ (BOOL)ty_stringIsEmpty:(NSString *)string trimBlankCharacter:(BOOL)trimFlag {
    if (!string) {
        return YES;
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    
    return [string ty_isEmptyShouldTrimBlankCharacter:trimFlag];
}

- (BOOL)ty_stringIsInteger {
    NSCharacterSet *numericOnly = [NSCharacterSet decimalDigitCharacterSet];
    NSCharacterSet *stringSet = [NSCharacterSet characterSetWithCharactersInString:self];
    
    if ([numericOnly isSupersetOfSet:stringSet]) {
        return YES;
    } else {
        return NO;
    }
}

- (unsigned int)ty_unsignedIntValue {
    NSNumberFormatter *formate = [[NSNumberFormatter alloc] init];
    [formate setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *number = [formate numberFromString:self];
    return [number unsignedIntValue];
}

- (UInt64)ty_unsignedLongLongValue {
    NSNumberFormatter *formate = [[NSNumberFormatter alloc] init];
    [formate setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *number = [formate numberFromString:self];
    return [number unsignedLongLongValue];
}

- (NSUInteger)ty_characterCount {
    if ([self length] == 0) {
        return 0;
    }
    
    __block NSUInteger count = 0;
    [self enumerateSubstringsInRange:NSMakeRange(0, self.length) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        count++;
    }];
    return count;
}

- (NSString *)ty_stringByGlossTelephone {
    NSString *resultString = @"";
    NSInteger length = [self length];
    if (length < 11) {
        return resultString;
    }
    
    if (![self ty_stringIsInteger]) {
        return resultString;
    }
    
    return [self stringByReplacingCharactersInRange:NSMakeRange(length-5-3, 5) withString:@"*****"];
}

- (NSString *)ty_pinyinString {
    NSMutableString *source = [self mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    return source;
}

- (NSString *)ty_pinyinFirstLetter {
    NSMutableString *source = [self mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    NSArray *substrings = [source componentsSeparatedByString:@" "];
    NSMutableString *firstLetter = [[NSMutableString alloc] init];
    for (NSString *string in substrings) {
        [firstLetter appendString:[string substringToIndex:1]];
    }
    return [firstLetter copy];
}

- (BOOL)ty_stringIsIPV4 {
    
    __unused struct in_addr pin;
    int success = inet_aton([self UTF8String], &pin);
    if (success == 1) {
        return YES;
    }
    return NO;
}

- (BOOL)ty_stringIsInternalIPV4 {
    if (![self ty_stringIsIPV4]) {
        return NO;
    }
    
    in_addr_t ip_addr = inet_addr([self UTF8String]);
    ip_addr = htonl(ip_addr);
    if ((ip_addr >= 0x0A000000 && ip_addr <= 0x0AFFFFFF ) ||
        (ip_addr >= 0xAC100000 && ip_addr <= 0xAC1FFFFF ) ||
        (ip_addr >= 0xC0A80000 && ip_addr <= 0xC0A8FFFF )
        )
    {
        return YES;
    }
    
    return NO;
}

@end
